  <template>
  <div id="home">
    <Header :check="3"
            @getUserInfo="getUserInfo" />
    <div class="clear_nav"></div>

    <div>
      <van-swipe :autoplay="3000"
                 lazy-render>
        <van-swipe-item v-for="image in images"
                        :key="image">
          <img :src="image.images_url"
               style="height: 240px; width: 100%; object-fit: cover" />
        </van-swipe-item>
      </van-swipe>
    </div>

    <div class="coin">
      <div class="top">
        <div class="left">
          <img src="../assets/img/nav/logo.png"
               alt="" />
          <div>
            <p>
              BOSS
              <span v-if="coinPrice != 0">↑{{
                  parseFloat(
                    ((1 / coinPrice - 0.00001) / 0.00001) * 100
                  ).toFixed(2)
                }}%</span>
            </p>
            <h5>
              $ {{ coinPrice > 0 ? parseFloat(1 / coinPrice).toFixed(8) : 0 }}
            </h5>
          </div>
        </div>
        <div class="right">
          <p>{{ $t("hl_inCoin") }}</p>
          <p>{{ $t("hl_onlyUp") }}</p>
        </div>
      </div>
      <div class="btn"
           @click="router.push('/trade')">
        {{ $t("hl_goTrade") }}
      </div>
    </div>

    <div class="section">
      <h1>{{ $t("hl_join") }}</h1>
      <h5>
        {{ $t("hl_note2_new") }}
      </h5>

      <div class="choose">
        <p>{{ $t("hl_chooseMoney") }}:</p>
        <div class="price">
          <h5 :class="sel_price == 0 ? 'p_sel' : 'p_un'"
              @click="sel_price = 0">
            20
          </h5>
          <h5 :class="sel_price == 1 ? 'p_sel' : 'p_un'"
              @click="sel_price = 1">
            100
          </h5>
          <h5 :class="sel_price == 2 ? 'p_sel' : 'p_un'"
              @click="sel_price = 2">
            1000
          </h5>
          <h5 :class="sel_price == 3 ? 'p_sel' : 'p_un'"
              @click="sel_price = 3">
            3000
          </h5>
        </div>
      </div>
      <p>预估需要: {{ sel_price_arr[sel_price] }}USDT</p>
      <p>
        USDT{{ $t("hl_balance") }}: {{ parseFloat(usdtBalance).toFixed(4) }}
      </p>
      <!-- <div class="btn" @click="goJoin()">{{ $t("hl_join") }}</div> -->
      <h1 style="color: red;">系统升级中，请暂停入金</h1>
      <h4 @click="router.push('/order')">{{ $t("hl_tab3") }}</h4>
    </div>

    <div class="share">
      <div class="title">{{ $t("hl_invitePool") }}</div>
      <div class="people">
        <h5>{{ $t("hl_poolNum") }}</h5>
        <p>{{ parseFloat(invitePool_b).toFixed(4) }}</p>
      </div>
      <div class="people">
        <h5>{{ $t("hl_openTime") }}</h5>
        <p>{{ winTime1 }}</p>
      </div>
      <div class="people">
        <h5>{{ $t("hl_cutTime") }}</h5>
        <p>
          {{ Math.floor(time1 / 86400) }}{{ $t("hl_day")
          }}{{ Math.floor((time1 % 86400) / 3600) }}{{ $t("hl_hour")
          }}{{ Math.floor(((time1 % 86400) % 3600) / 60) }}{{ $t("hl_minute")
          }}{{ Math.floor(((time1 % 86400) % 3600) % 60) }}{{ $t("hl_second") }}
        </p>
      </div>
      <div class="people">
        <h6 v-if="systemPartner"
            @click="clickInvitePool()">
          {{ $t("hl_open") }}
        </h6>
        <h5 v-else></h5>
        <h6 @click="showPeopleList(0)">{{ $t("hl_seeWin") }}</h6>
      </div>
    </div>

    <div class="share">
      <div class="title">{{ $t("hl_partenrPool") }}</div>
      <div class="people">
        <h5>{{ $t("hl_poolNum") }}</h5>
        <p>{{ parseFloat(parnterPool_b).toFixed(4) }}</p>
      </div>
      <div class="people">
        <h5>{{ $t("hl_openTime") }}</h5>
        <p>{{ winTime2 }}</p>
      </div>
      <div class="people">
        <h5>{{ $t("hl_cutTime") }}</h5>
        <p>
          {{ Math.floor(time2 / 86400) }}{{ $t("hl_day")
          }}{{ Math.floor((time2 % 86400) / 3600) }}{{ $t("hl_hour")
          }}{{ Math.floor(((time2 % 86400) % 3600) / 60) }}{{ $t("hl_minute")
          }}{{ Math.floor(((time2 % 86400) % 3600) % 60) }}{{ $t("hl_second") }}
        </p>
      </div>
      <div class="people">
        <h6 v-if="systemPartner"
            @click="clickPartnerPool()">
          {{ $t("hl_open") }}
        </h6>
        <h5 v-else></h5>
        <h6 @click="showPeopleList(1)">{{ $t("hl_seeWin") }}</h6>
      </div>
    </div>

    <!-- <div class="space"></div> -->

    <div class="section">
      <h1>{{ $t("hl_coin") }}</h1>

      <div class="box">
        <p>{{ $t("hl_memo") }}: BOSS</p>

        <div>
          <p>{{ $t("hl_conAddr") }}: {{ api.coinAddress }}</p>
          <h6 @click="copy(api.coinAddress)">{{ $t("hl_copy") }}</h6>
        </div>
        <p>{{ $t("hl_sendRull") }}</p>
        <p>
          {{ $t("hl_pullAmount") }}:
          {{ Number(canUseAmount) - Number(usedAmount) }}
        </p>
        <p>{{ $t("hl_node3") }}</p>
      </div>

      <div class="persent">
        <h1>{{ $t("hl_pullBurnLine") }}</h1>
        <h1>
          {{ $t("hl_pullTotal") }}:
          {{ Number(canUseAmount) }}
        </h1>
        <div class="line_per">
          <div class="line">
            <div class="used"
                 :style="{ width: lineWidth_t }"></div>
          </div>
          <p>{{ lineWidth }}%</p>
        </div>

        <h4>
          {{ $t("hl_burnTotal") }}:{{ parseFloat(usedAmount).toFixed(4) }}
        </h4>
      </div>
    </div>

    <template v-if="showPeople">
      <div class="level_pop">
        <van-popup v-model:show="showPeople"
                   round
                   :style="{ width: '90%', height: '40%' }">
          <div class="level">
            <h1>{{ $t("hl_winAddr") }}</h1>
            <div class="peopleList">
              <p>序号</p>
              <p>地址</p>
            </div>
            <div class="peopleList"
                 v-for="(item, index) in peopleList"
                 :key="index">
              <p>{{ index + 1 }}</p>
              <h3>
                {{
                  item.slice(0, 5) +
                  "..." +
                  item.slice(item.length - 4, item.length)
                }}
              </h3>
            </div>
          </div>
        </van-popup>
      </div>
    </template>

    <div class="space"></div>
  </div>
</template>

<script setup>
// import { api_getBanner } from "../request/api";
import { ref, reactive } from "vue";
import {
  showFailToast,
  showSuccessToast,
  showLoadingToast,
  closeToast,
  showToast,
} from "vant";
import { getFloat, getDotcAllowance, getEstimateGas } from "../utils/common";
import { useRouter } from "vue-router";
import api from "../dapp/api";
import i18n from "@/lang";
import useClipboard from "vue-clipboard3"; // 复制组件
const { toClipboard } = useClipboard();

const router = useRouter();
let lineWidth = ref(0);
let lineWidth_t = ref("60%");
let address = ref("");
let images = ref([
  {
    images_url: require("../assets/img/index/b1.jpeg"),
  },
  {
    images_url: require("../assets/img/index/b2.jpeg"),
  },
  {
    images_url: require("../assets/img/index/b3.jpeg"),
  },
]);
let sel_price = ref(0);
let sel_price_arr = ref([26, 130, 1300, 3900]);
let peopleList1 = ref([]);
let peopleList2 = ref([]);
let peopleList = ref([]);

let canUseAmount = ref(0);
let usedAmount = ref(0);
let usdtBalance = ref(0);
let coinPrice = ref(0);

let showPeople = ref(false);
let systemPartner = ref(false);
let invitePool_b = ref(0);
let parnterPool_b = ref(0);
let gasPrice = ref(0);
let timeInfo = ref({});

let conContract, usdtContract, coinContract, web3;
// 那得的数据
function getUserInfo (obj) {
  address.value = obj.address;
  // console.log("====", obj);

  web3 = obj.web3;

  conContract = new obj.web3.eth.Contract(conAbi, api.conAddress);
  usdtContract = new obj.web3.eth.Contract(tongAbi, api.usdtAddress);
  coinContract = new obj.web3.eth.Contract(ctcAbi, api.coinAddress);

  systemPartner.value = obj.userData.systemPartner;
  // systemPartner.value = true;
  gasPrice.value = obj.gasPrice;

  coinContract.methods
    .balanceOf(api.invitePool)
    .call()
    .then(async (res) => {
      invitePool_b.value = obj.web3.utils.fromWei(res, "ether");
    });

  coinContract.methods
    .balanceOf(api.parnterPool)
    .call()
    .then(async (res) => {
      // console.log("parnterPool_b=", res);
      parnterPool_b.value = obj.web3.utils.fromWei(res, "ether");
    });

  conContract.methods
    .getpartnerpoolAddress()
    .call()
    .then(async (res) => {
      // console.log("1111=", res);
      peopleList2.value = res;
    });

  conContract.methods
    .getrecommendpoollAddress()
    .call()
    .then(async (res) => {
      // console.log("2222=", res);
      peopleList1.value = res;
    });

  conContract.methods
    .GettimeInfo()
    .call()
    .then(async (res) => {
      timeInfo.value = res;
      checkTime();
    });

  conContract.methods
    .getTokenPrice()
    .call()
    .then(async (res) => {
      coinPrice.value = parseFloat(Number(res) / 10000).toFixed(4);
    });

  reload_data();
}

function reload_data () {
  conContract.methods
    .getCTCBalance()
    .call()
    .then(async (res) => {
      canUseAmount.value = web3.utils.fromWei(res, "ether");

      coinContract.methods
        .balanceOf("0x0000000000000000000000000000000000000001")
        .call()
        .then(async (res) => {
          // console.log("usedAmount=", res);
          usedAmount.value = web3.utils.fromWei(res, "ether");
          lineWidth.value = parseFloat(
            (Number(usedAmount.value) / Number(canUseAmount.value)) * 100
          ).toFixed(4);
          lineWidth_t.value = lineWidth.value + "%";
        });
    });

  usdtContract.methods
    .balanceOf(address.value)
    .call()
    .then(async (res) => {
      usdtBalance.value = web3.utils.fromWei(res, "ether");
    });
}

async function goJoin () {
  if (!conContract)
  {
    console.log("?");
    return;
  }

  let TwoTime = Number(sessionStorage.getItem("t_goJoin"));
  let news = new Date().getTime();
  if (news - TwoTime < 2000)
  {
    return;
  }
  sessionStorage.setItem("t_goJoin", new Date().getTime());

  if (Number(usdtBalance.value) < sel_price_arr.value[sel_price.value])
  {
    showFailToast(i18n.global.t("hl_noMoney"));
    return;
  }

  await getDotcAllowance(address.value, usdtContract, api.conAddress);

  try
  {
    const aa = conContract.methods._buy(sel_price.value);

    const bb = await getEstimateGas(aa, address.value);

    await aa
      .send({
        from: address.value,
        gas: bb || 9000000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          // location.reload();
          reload_data();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e)
  {
    console.log("e :>> ", e);
  }
}

const copy = async (url) => {
  if (url != null && url != "")
  {
    await toClipboard(url);
    showToast(i18n.global.t("copy_suc"));
  }
};

function showPeopleList (index) {
  showPeople.value = true;
  if (index == 0)
  {
    peopleList.value = peopleList1.value;
  } else
  {
    peopleList.value = peopleList2.value;
  }
}

let time1 = ref(0);
let time2 = ref(0);
let timeCut1 = ref(null);
let timeCut2 = ref(null);
let winTime1 = ref(0);
let winTime2 = ref(0);
function checkTime () {
  const currentDate = new Date();
  let nowT = currentDate.getTime();

  // timeInfo
  let t1 = Number(timeInfo.value[0]);
  let t2 = Number(timeInfo.value[1]);
  let t3 = Number(timeInfo.value[2]);
  let t4 = Number(timeInfo.value[3]);

  // console.log("currentDate", currentDate, nowT);
  // console.log("timeInfo", timeInfo);

  time1.value = ((t1 + t2) * 1000 - Number(nowT)) / 1000;
  time2.value = ((t3 + t4) * 1000 - Number(nowT)) / 1000;
  // console.log("time1=", time1.value);
  // console.log("time2=", time2.value);

  winTime1.value = timestampToTime(Number(t1 * 1000) + Number(t2 * 1000));
  winTime2.value = timestampToTime(Number(t3 * 1000) + Number(t4 * 1000));

  timeCut1.value = setInterval(() => {
    time1.value--;

    if (time1.value <= 0)
    {
      clearInterval(time1.value);
      time1.value = null;
    }
  }, 1000);

  timeCut2.value = setInterval(() => {
    time2.value--;

    if (time2.value <= 0)
    {
      clearInterval(time2.value);
      time2.value = null;
    }
  }, 1000);
}

function timestampToTime (timestamp) {
  var date = new Date(timestamp);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  return (
    year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  );
}

function clickInvitePool () {
  try
  {
    conContract.methods
      .partnerpoolAllocation()
      .send({
        from: address.value,
        gas: 10000000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e)
  {
    console.log("e :>> ", e);
  }
}

function clickPartnerPool () {
  try
  {
    conContract.methods
      .recommendpoolAllocation()
      .send({
        from: address.value,
        gas: 10000000,
        gasPrice: gasPrice.value,
      })
      .on("transactionHash", (hash) => {
        showLoadingToast({
          message: i18n.global.t("hl_waitLink"),
          forbidClick: true,
          duration: 0,
        });
      })
      .on("receipt", (receipt) => {
        closeToast();
        showSuccessToast(i18n.global.t("hl_opSucc"));
        setTimeout(() => {
          closeToast();
          location.reload();
        }, 2500);
      })
      .on("error", (error, receipt) => {
        closeToast();
        showFailToast(i18n.global.t("hl_opFail"));
      });
  } catch (e)
  {
    console.log("e :>> ", e);
  }
}
</script>

<style scoped lang="less">
#home {
  padding: 10px 3% 0;
  background-color: #fff;
  min-height: 100vh;

  :deep(.van-swipe, .van-swipe-item) {
    margin-bottom: 15px;
    border-radius: 10px;

    img {
      width: 100%;
    }
  }

  .coin {
    width: 100%;
    padding: 3%;
    background: #f2f2f2;
    border-radius: 4px;
    box-shadow: 1px 1px 3px 1px #999;
    margin-bottom: 20px;

    .top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 20px;

      .left {
        display: flex;
        align-items: center;

        p {
          line-height: 18px;
        }

        span {
          margin-left: 12px;
          font-size: 10px;
          color: #1296db;
          line-height: 18px;
        }

        img {
          height: 40px;
          margin-right: 12px;
        }
      }

      .right {
        p {
          font-size: 16px;
        }
      }
    }

    .btn {
      width: 33%;
      margin: 0 auto;
      text-align: center;
      padding: 4px;
      border: 1px solid #1296db;
      color: #1296db;
      font-size: 16px;
      border-radius: 4px;
    }
  }

  .section {
    width: 100%;
    padding: 3%;
    h1 {
      width: 100%;
      text-align: center;
      margin-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
    }
    h5 {
      font-size: 12px;
      color: #696565;
    }

    .choose {
      p {
        margin: 8px 0;
        color: #1296db;
        font-size: 16px;
      }

      .price {
        display: flex;
        align-items: center;
        margin: 8px 0;
        h5 {
          width: 88px;
          height: 40px;
          border-radius: 4px;
          line-height: 40px;
          text-align: center;
          font-size: 16px;
          margin-right: 20px;
        }
        .p_sel {
          background: #1296db;
          color: white;
        }

        .p_un {
          background: white;
          border: 1px solid #1296db;
          color: #1296db;
        }
      }
    }

    .btn {
      width: 50%;
      height: 40px;
      border-radius: 4px;
      margin: 12px auto;
      background: #1296db;
      color: white;
      line-height: 40px;
      text-align: center;
    }

    h4 {
      width: 100%;
      height: 40px;
      color: #1296db;
      text-align: center;
      line-height: 40px;
    }

    .box {
      p {
        color: #696565;
        font-size: 16px;
        line-height: 20px;
        margin-bottom: 6px;
      }
      div {
        display: flex;

        p {
          flex: 1;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin-right: 12px;
        }
        h6 {
          color: #1296db;
          width: 52px;
          text-align: center;
          font-weight: bold;
        }
      }
    }

    .persent {
      width: 100%;
      padding: 3%;
      background: #f2f2f2;
      border-radius: 4px;
      box-shadow: 1px 1px 3px 1px #999;
      margin: 20px 0;

      h1 {
        width: 100%;
        text-align: center;
        font-size: 18px;
        margin-bottom: 6px;
      }

      h4 {
        width: 100%;
        text-align: center;
        font-size: 16px;
        color: #1296db;
        margin-bottom: 6px;
      }

      .line_per {
        display: flex;
        align-items: center;
        justify-content: space-between;
        align-items: center;
        width: 100%;

        .line {
          width: 90%;
          margin-right: 20px;
          height: 20px;
          background: white;
          box-shadow: 1px 1px 0px 0px #999;
          .used {
            height: 100%;
            background: #1296db;
            width: 30%;
          }
        }
      }
    }

    .ecology {
      img {
        width: 100%;
        margin-bottom: 6px;
      }

      h5 {
        width: 100%;
        white-space: pre-wrap;
        word-break: break-all;
        word-wrap: break-word;
      }

      p {
        margin: 10px 0;
        padding: 4px 10px;
        border-radius: 4px;
        background: #1296db;
        color: white;
        font-size: 16px;
        display: inline-block;
      }
    }
  }

  .space {
    width: 100%;
    height: 4px;
    background: #d9d9d9;
  }

  .pop {
    padding: 5%;
    border-top-right-radius: 30px;
    border-top-left-radius: 30px;

    .title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;
      margin-bottom: 12px;
      padding-bottom: 12px;
      p {
        font-size: 20px;
        color: #606060;
      }

      img {
        height: 28px;
      }
    }

    .name {
      display: flex;
      align-items: center;
      justify-content: space-between;

      p {
        font-size: 15px;
        color: #606060;
      }
      h5 {
        font-size: 13px;
        color: #606060;
      }
    }

    input {
      border: 2px solid rgba(0, 194, 152, 0.25);
      height: 40px;
      margin: 12px 0;
      border-radius: 8px;
      width: 100%;
      box-shadow: 0.5px 0.5px #1296db;
    }

    textarea {
      border: 2px solid rgba(0, 194, 152, 0.25);
      height: 60px;
      margin: 12px 0;
      border-radius: 8px;
      width: 100%;
      padding: 2%;
      box-shadow: 0.5px 0.5px #1296db;
    }

    .btn {
      width: 100%;
      height: 40px;
      line-height: 40px;
      color: white;
      background: #1296db;
      text-align: center;
      border-radius: 8px;
      margin: 20px 0;
    }

    h4 {
      font-size: 11px;
      color: #606060;
      font-weight: 400;
    }
  }

  .share {
    width: 100%;
    padding: 3%;
    background: #f2f2f2;
    border-radius: 4px;
    box-shadow: 1px 1px 3px 1px #999;
    margin-bottom: 20px;

    .title {
      width: 100%;
      font-size: 20px;
      text-align: center;
    }

    .people {
      display: flex;
      align-items: center;
      justify-content: space-between;

      p {
        margin: 8px 0;
        color: #1296db;
        // font-size: 16px;
      }

      h5 {
        margin: 8px 0;
      }

      h6 {
        height: 40px;
        line-height: 40px;
        color: white;
        background: #1296db;
        text-align: center;
        border-radius: 8px;
        // margin: 12px 0;
        padding: 0 24px;
      }
    }
    .seeBtn {
      text-align: right;
      width: 100%;
    }
  }

  .level_pop {
    border-radius: 18px;
    overflow: hidden;
    width: 100%;
    margin: 0 auto;
    position: relative;

    .level {
      width: 100%;
      padding: 12px;

      h4 {
        color: #1296db;
        margin-bottom: 12px;
      }

      h1 {
        font-size: 18px;
        text-align: center;
        margin-bottom: 20px;
      }

      .peopleList {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 3%;
        width: 100%;
        margin-bottom: 12px;
        p {
          width: 30%;
          text-align: center;
        }

        h3 {
          width: 50%;
          text-align: center;
          background: #d9d9d9;
          text-align: center;
          margin-bottom: 12px;
          padding: 4px;
          border-radius: 4px;
          white-space: pre-wrap;
          word-break: break-all;
          word-wrap: break-word;
        }
      }

      .btn {
        display: flex;
        align-items: center;
        justify-content: space-between;

        h5 {
          border: 1px solid #1296db;
          width: 20%;
          border-radius: 4px;
          color: #1296db;
          text-align: center;
        }

        p {
          background: #1296db;
          width: 20%;
          border-radius: 4px;
          color: white;
          text-align: center;
        }
      }

      .sel_1 {
        margin-bottom: 140px;
      }

      .sel_2 {
        margin-bottom: 20px;
      }
      .choose {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 12px;
        height: 44px;
        border: 1px solid black;
        line-height: 44px;
        background: white;
        position: relative;

        p {
          flex: 1;
        }
        .selectList {
          position: absolute;
          left: 0;
          top: 44px;
          width: 100%;
          max-height: 120px;
          background: white;
          overflow: auto;
          .sel_cell {
            height: 30px;
            line-height: 30px;
            border-bottom: 1px solid #d9d9d9;
            padding: 0 12px;
          }
        }

        img {
          height: 36px;
        }
      }
    }

    .levelBtn {
      display: flex;
      align-content: center;
      justify-content: space-between;
      margin-bottom: 12px;
      p {
        background: #1296db;
        color: white;
        padding: 0 24px;
        border-radius: 8px;
        line-height: 36px;
        height: 36px;
      }
      h5 {
        color: #1296db;
        border: 1px solid #1296db;
        padding: 0 24px;
        border-radius: 8px;
        line-height: 36px;
        height: 36px;
      }
    }
  }
}
</style>
